package com.ft.oa.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.project.ExecProjectTask;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.project.ExecProjectTaskMapper;
import com.ft.oa.service.project.ExecProjectTaskService;
import com.ft.oa.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 项目立项任务
 *
 * @author hefeng
 * @ClassName ExecProjectTaskController
 * @date 2022/1/6 16:48
 */
@Service
public class ExecProjectTaskServiceImpl extends ServiceImpl<ExecProjectTaskMapper, ExecProjectTask> implements ExecProjectTaskService {

    private Logger log = LoggerFactory.getLogger(getClass());

    /**
     * param execProjectTask
     * return ApiResult
     * describe 项目任务新建
     *
     * @author 禾丰
     * @date 2022/1/6
     */
    @Override
    public ApiResult add(ExecProjectTask execProjectTask) {
        try {
            baseMapper.insert(execProjectTask);
        } catch (Exception e) {
            log.error("[项目新建任务操作] error---{}", e.getMessage());
            e.printStackTrace();
            return ApiResult.fail();
        }
        return ApiResult.simpleSuccess();
    }

    /**
     * param execProjectTask
     * return ApiResult
     * describe 项目任务编辑
     *
     * @author 禾丰
     * @date 2022/1/6
     */
    @Override
    public ApiResult update(ExecProjectTask execProjectTask) {
        try {
            baseMapper.updateById(execProjectTask);
        } catch (Exception e) {
            log.error("[项目任务编辑操作] error---{}", e.getMessage());
            e.printStackTrace();
            return ApiResult.fail();
        }
        return ApiResult.simpleSuccess();
    }

    /**
     * param id
     * return ExecSubProject
     * describe 项目任务详情
     *
     * @author 禾丰
     * @date 2022/1/6 11:31
     */
    @Override
    public ExecProjectTask detail(Long id) {
        ExecProjectTask execProjectTask = null;
        try {
            execProjectTask = baseMapper.selectById(id);
        } catch (Exception e) {
            log.error("[项目任务详情页] error---{}", e.getMessage());
            e.printStackTrace();
        }
        return execProjectTask;
    }

    /**
     * param name
     * param execProjectId
     * param execProjectName
     * param page
     * param size
     * return ApiResult
     * describe 项目任务列表
     *
     * @author 禾丰
     * @date 2022/1/7 10:27
     */
    @Override
    public Page<ExecProjectTask> list(String name, Long execProjectId, String execProjectName, int page, int size) {
        try {
            QueryWrapper<ExecProjectTask> execProjectTaskWrapper = new QueryWrapper<>();
            execProjectTaskWrapper.eq("del", 0);
            if (StringUtils.isNotBlank(name)) {
                execProjectTaskWrapper.like("name", name);
            }
            if (null != execProjectId) {
                execProjectTaskWrapper.eq("exec_project_id", execProjectId);
            }
            if (StringUtils.isNotBlank(execProjectName)) {
                execProjectTaskWrapper.like("name", execProjectName);
            }
            if(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager())){
                execProjectTaskWrapper.eq("creator",SecurityUtils.getCurrentUser().getId());
            }
            Page<ExecProjectTask> execSubProjectPage = baseMapper.selectPage(new Page<>(page, size), execProjectTaskWrapper);
            return execSubProjectPage;
        } catch (Exception e) {
            log.error("[项目任务列表页] error---{}", e.getMessage());
            e.printStackTrace();
        }
        return new Page<>(page, size);
    }

    /**
     * param id
     * return ExecSubProject
     * describe 项目任务删除
     *
     * @author 禾丰
     * @date 2022/1/7 10:53
     */
    @Override
    public ApiResult del(Long id) {
        try {
            UpdateWrapper<ExecProjectTask> execProjectTaskWrapper = new UpdateWrapper();
            execProjectTaskWrapper.set("del", 1);
            execProjectTaskWrapper.eq("id", id);
            baseMapper.update(new ExecProjectTask(), execProjectTaskWrapper);
        } catch (Exception e) {
            log.error("[项目任务删除] error---{}", e.getMessage());
            e.printStackTrace();
            return ApiResult.fail();
        }
        return ApiResult.simpleSuccess();
    }


}
